package org.bagou.xiangs.framework_mvp.presenter;

import android.content.Context;
import android.content.res.AssetManager;

import com.google.gson.Gson;

import org.bagou.xiangs.framework_mvp.model.WechatInfo;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class MainMvpPresenter extends MainMvpContract.MainMvpPresenter {
    @Override
    public void getUserInfoPromote() {
        WechatInfo wechatInfo = getWechatInfo(view.context());
        view.userInfoCallback(wechatInfo);

    }

    /**
     * this s
     * @param context
     * @return
     */
    private WechatInfo getWechatInfo(Context context) {
        AssetManager assetManager = context.getAssets();
        try {
            InputStreamReader reader = new InputStreamReader(assetManager.open("mvvm.json"));
            BufferedReader bufferedReader = new BufferedReader(reader);
            String line;
            StringBuffer stringBuffer = new StringBuffer();
            while ((line = bufferedReader.readLine()) != null) {
                stringBuffer.append(line);
            }
            bufferedReader.close();
            reader.close();

            Gson gson = new Gson();
            WechatInfo wechatInfo = gson.fromJson(stringBuffer.toString(), WechatInfo.class);
            return wechatInfo;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }
}
